package com.zhuss.core.processor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.config.SearchConfigBo;
import com.zhuss.bean.vo.core.config.ListConfigVo;
import com.zhuss.common.ListVo;
import com.zhuss.common.PageVo;
import com.zhuss.core.db.dao.ext.ConfigDao;
import com.zhuss.core.db.dao.ext.ConfigTypeDao;
import com.zhuss.core.db.entity.Config;
import com.zhuss.core.db.entity.ConfigType;
import com.zhuss.exception.OceanException;
import com.zhuss.exception.ExceptionCode;


/**
 * 查询配置 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/20 21:55
 */
@Component("configRead")
public class ConfigRead {
	
	@Autowired
	private ConfigDao configDao;
	
	
	@Autowired
	private ConfigTypeDao configTypeDao;
	
	
	/**
	 * 查询最大的key值
	 * @param 
	 * @return
	 */
	public Integer findMaxKeyIncr(String type) {
		List<Config> result = configDao.listAll(type);
		Collections.sort(result, new Comparator<Config>() {

			@Override
			public int compare(Config o1, Config o2) {
				return o2.getConfigKey() - o1.getConfigKey();
			}
		});
		Integer configKey = 0;
		if (result.size() > 0) {
			configKey = result.get(0).getConfigKey();
		}
		return configKey + 10;
	}
	
	
	/**
	 * 查询最大的sort
	 * @param  
	 * @param 
	 * @return
	 */
	public Integer getMaxSort(String type) {
		List<Config> list = configDao.listAll(type);
		Integer sort = 0;
		for (Config c : list) {
			Integer s = c.getSort();
			if (s > sort) {
				sort = s;
			}
		}
		return sort;
	}
	
	
	public Config getBySort(String type, Integer sort) {
		Config result = null;
		List<Config> list = configDao.listAll(type);
		for (Config c : list) {
			if (c.getSort().equals(sort)) {
				result = c;
			}
		}
		return result;
	}
	
	
	/**
	 * 根据sort查询配置
	 * @param  
	 * @param 
	 * @return
	 */
	public List<Config> listBySort(String type, Integer sort) {
		List<Config> result = new ArrayList<Config>();
		List<Config> list = configDao.listAll(type);
		for (Config c : list) {
			if (c.getSort().equals(sort)) {
				result.add(c);
			}
		}
		return result;
	}
	
	/**
	 * 根据sort查询配置
	 * @param  
	 * @param 
	 * @return
	 */
	public List<Config> listNotIdBySort(String type, Integer sort, Long configId) {
		List<Config> result = new ArrayList<>();
		List<Config> list = configDao.listAll(type);
		for (Config c : list) {
			if (c.getSort().equals(sort) && !c.getConfigId().equals(configId)) {
				result.add(c);
			}
		}
		return result;
	}
	
	/**
	 * 根据value查询配置
	 * @param  
	 * @param 
	 * @return
	 */
	public List<Config> list(String type, String value) {
		List<Config> result = configDao.listByValue(type, value);
		return result;
	}
	
	public List<Config> listNotId(String type, String value, Long configId) {
		List<Config> result = new ArrayList<Config>();
		List<Config> list = list(type, value);
		for (Config c : list) {
			if (!c.getConfigId().equals(configId)) {
				result.add(c);
				break;
			}
		}
		return result;
	}
	
	
	/**
	 * 配置ID列表
	 * @return
	 */
	public Object listConfigKey(String type) {
		List<Config> configList = configDao.listAll(type);
		List<Long> result = new ArrayList<>();
		for (Config config : configList) {
			result.add(config.getConfigId());
		}
		return result;
	}
	
	/**
	 * 配置
	 * @return
	 */
	public Object listConfig(String type) {
		List<Config> configList = configDao.listAll(type);
		List<ListVo> result = new ArrayList<>();
		transferList(configList, result);
		return result;
	}
	
	/**
	 * 配置 
	 * @return
	 */
	public Object listConfigId(String type) {
		List<Config> configList = configDao.listAll(type);
		List<Long> result = new ArrayList<>();
		transferListId(configList, result);
		return result;
	}
	
	private void transferListId(List<Config> configList, List<Long> result) {
		for (Config item : configList) {
			result.add(item.getConfigId());
		}
	}

	private void transferList(List<Config> configList, List<ListVo> result) {
		for (Config item : configList) {
			ListVo vo = new ListVo();
			vo.setKey(item.getConfigKey().longValue());
			vo.setLabel(item.getValue());
			vo.setTag(item.getTag());
			result.add(vo);
		}
		
	}

	/**
	 * 配置列表
	 * @param bo
	 * @return
	 */
	public Object list(SearchConfigBo bo) {
//		bo.setPageSize(Integer.MAX_VALUE);
		PageVo<ListConfigVo> result = new PageVo<ListConfigVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(transfer(configDao.list(bo)));
        result.setTotal(configDao.countList(bo));
		return result;
	}
	
	private List<ListConfigVo> transfer(List<ListConfigVo> list) {
		for (ListConfigVo item : list) {
			List<ConfigType> typeList = configTypeDao.listAll(item.getType());
			if (!typeList.isEmpty()) {
				item.setTypeName(typeList.get(0).getValue());
			}
		}
		return list;
	}


	/**
	 * 配置详情
	 * @param configId
	 * @return
	 * @throws OceanException
	 */
	public Object detail(Long configId) throws OceanException {
		Object result = configDao.detail(configId);
		if (result == null) {
			throw new OceanException(ExceptionCode.数据不存在);
		}
		return result;
	}


}
